package com.young.shop.admin.service.impl;

import com.young.shop.admin.dao.OrderMapper;
import com.young.shop.admin.exceptions.BaseException;
import com.young.shop.admin.service.OrderService;
import com.young.shop.pojo.entity.Order;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.OrderServiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Override
	public List<Order> getOrderList(String beforeDate, String afterDate) {
		//判断Date是否为空,为空赋值
		if (beforeDate == null || "".equals(beforeDate.trim())) {
			beforeDate = "1970-01-01";
		}
		if (afterDate == null ||"".equals(afterDate.trim())) {
			afterDate = "2999-01-01";
		}
		//根据时间查询订单列表
		return orderMapper.selectOrderList(beforeDate, afterDate);

	}

	@Override
	public int sendOrder(Integer id, Integer status) {
		//判断id是否为空
		if (id == null) {
			throw new RuntimeException("系统错误");
		}
		//生成时间
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateFormat = sd.format(new Date());
		//根据用户id修改用户信息
		int i = orderMapper.updateOrder(id, status,dateFormat);
		if (i != 1) {
			throw new RuntimeException("系统错误");
		}
		return i;
	}

	@Override
	public int cancelOrder(Integer id, Integer status) {
		//判断id是否为空
		if (id == null) {
			throw new RuntimeException("系统错误");
		}
		//生成时间
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateFormat = sd.format(new Date());
		//根据用户id修改用户信息
		int i = orderMapper.updateOrderCancelDate(id, status,dateFormat);
		if (i != 1) {
			throw new RuntimeException("系统错误");
		}
		return i;
	}

	@Override
	public List<OrderServiceVO> orderDetail(Integer id) {
		//判断id是否为空
		if (id == null) {
			throw new RuntimeException("系统错误");
		}
		//根据id查询商品详细
		return orderMapper.selectOrderDetail(id);
	}
}
